package com.intsig.fireworkact;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.intsig.pdf.sdk.PdfConvertUtil;
import com.intsig.pdfengine.PDF_Engine;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ImagesToPdf {

    //final static String appkey="586ee724e16b12fac0712c0b10-vagfvt";


//TextView content_id;

    @SuppressLint("StaticFieldLeak")
    public void saveImagesToPdfFunc(final String pdfpathString, final Application application,final String appkey, final List<String> listImages,final Activity activity) {

        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {
                //mIDCardScanSDK.setSaveImageFileName("foxcen.jpg");

                //String  pdfpathString=Environment.getExternalStorageDirectory()+"/test.pdf";
                int ret = 0;
                File f = new File(pdfpathString);
                if (f.exists())
                    f.delete();
                else {
                    try {
                        f.createNewFile();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                Log.d("pdf路径",pdfpathString);
                PdfConvertUtil engine = new PdfConvertUtil();
//				engine.setLogLevel(engine.LOG_DEBUG);


                int width = 800;
                int height = 1000;
                //The first step is to initialize the engine.
                ret = engine.initPDFEngine(application, appkey);


                if (ret != 0) {
                    Log.d("code:",ret+"");
                    System.out.println("init code:" + ret);
                    return ret+"";
                }

                //The second step is to create canvas.
                engine.createPDFCanvas(application,
                        pdfpathString, width, height);
                int i=1;
                for (String picpathString : listImages) {
                    //String picpathString = Environment.getExternalStorageDirectory() + "/aa.jpg";
                    File filePic = new File(picpathString);


                    BitmapFactory.Options opts = new BitmapFactory.Options();
                    opts.inJustDecodeBounds = true;
                    Bitmap bitmap= BitmapFactory.decodeFile(picpathString,opts);
                    int widthB=opts.outWidth;
                    int heightB=opts.outHeight;

                    int maxW=Math.max(widthB,heightB);
                    Double scale=0.0;


                    if (filePic.exists()) {
                        //The third step is to create page.
                        engine.newPage(width, height);

                        //The fourth step is to add pictures to canvas and add multiple page and pictures.

                        if(widthB>heightB) {
                            int w=(int)(800*heightB/widthB);
                            int fx=400-w/2;
                            engine.drawImage(picpathString, 0, fx, 800, w);
                        }else{
                            int w=(int)(800*widthB/heightB);
                            int fx=400-w/2;
                            engine.drawImage(picpathString, fx, 10, w, 800);

                        }
                    }

                    engine.setFont("freescan", 22);
                    engine.drawText("第"+i+"页", 380, 10, PDF_Engine.TS_ITALIC);
                    engine.setTextRenderingMode(PDF_Engine.FILL);

				i++;
                }

                //The fifth step: save the PDF file.
                engine.save2PdfFile();
                //The sixth step: releasing memory.
                engine.releasePDFEngine();


                return pdfpathString;
            }

            @Override
            protected void onPostExecute(String result) {
                if (result !=null&&result.length()>4) {
//					content_id.setText("提示：pdf文件成功ok");

                    Intent  intent=new Intent(activity,ShowPdfActivity.class);
                    intent.putExtra(ShowPdfActivity.PDF_RESULT_DIR,result);
                    activity.startActivity(intent);

                } else {
//					content_id.setText("提示：pdf文件失败 code:"+result);
                    Toast.makeText(
                            activity,
                            "授权失败:"+result,
                            Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();


    }




}
